<template>
  <div class="divLogin">
    <div class="divContent">
      <div class="divLeft">
        <div class="divTitle">
          <img src="@/assets/login/title.png" />
        </div>
        <el-form
          :model="loginData"
          :rules="rules"
          ref="ruleForm"
          @submit.native.prevent
        >
          <el-form-item prop="user">
            <div class="divUser">
              <div class="divImg"><img src="@/assets/login/mobile.png" /></div>
              <div class="divInput">
                <el-input
                  v-model="loginData.user"
                  ref="username"
                  placeholder="请输入内容"
                ></el-input>
              </div>
            </div>
          </el-form-item>
          <el-form-item prop="pwd">
            <div class="divPwd">
              <div class="divImg"><img src="@/assets/login/pwd.png" /></div>
              <div class="divInput">
                <el-input
                  v-model="loginData.pwd"
                  ref="password"
                  placeholder="请输入密码"
                  show-password
                  @keyup.enter.native="handleLogin"
                ></el-input>
              </div>
            </div>
          </el-form-item>
        </el-form>
        <div class="divCheckBox">
          <el-checkbox v-model="checked">记住密码</el-checkbox>
        </div>
        <div class="divBtnLogin">
          <el-button :loading="loading" @click.native.prevent="handleLogin"
            >立即登录</el-button
          >
        </div>
        <div class="gentleReminder">
            仅用于IT培训教学使用，为保障您的个人信息安全，请勿向平台录入任何个人敏感信息（如手机号、身份证号等）！
        </div>
      </div>
      <div class="divRight"></div>
    </div>
  </div>
</template>
<script lang="ts">
import { Input } from 'element-ui'
import { Vue, Component, Watch } from 'vue-property-decorator'
import { Dictionary, Route } from 'vue-router/types/router'
import { UserModule } from '@/store/modules/user'
import { validateLoginPwd, validateLoginUser } from '@/utils/validate'
import { ElForm } from 'element-ui/types/form'
import {userInfo} from 'os'
// 这个是必须的，去掉双向绑定就会失效
@Component({
  name: 'Login'
})
export default class extends Vue {
  private checked = false
  private otherQuery: Dictionary<string> = {}
  private redirect?: string
  private loading = false

  private loginData = {
    user: 'demo',
    pwd: '888itcast.CN764%...'
  }

  private rules = {
    user: [
      { required: true, validator: validateLoginUser, trigger: 'blur' }],
    pwd: [
      { required: true, validator: validateLoginPwd, trigger: 'blur' }]
  }

  mounted() {
    if (this.loginData.user === '') {
      (this.$refs.username as Input).focus()
    } else if (this.loginData.pwd === '') {
      (this.$refs.password as Input).focus()
    }
  }

  @Watch('$route', { immediate: true })
  private onRouteChange(route: Route) {
    const query = route.query as Dictionary<string>
    if (query) {
      this.redirect = query.redirect
      this.otherQuery = this.getOtherQuery(query)
    }
  }

  private handleLogin() {
    (this.$refs.ruleForm as ElForm).validate(async(valid:boolean) => {
      if (valid) {
        // this.loading = true
        // await UserModule.Login()
        const userInfo = {
          username: this.loginData.user,
          password: this.loginData.pwd
        }

        await UserModule.Login(userInfo)
        if (process.env.NODE_ENV === 'production') {
          // 生产环境跳转到项目课程简介页
          const query = encodeURIComponent(`from=${location.origin}/&resource=https://v.itheima.net/project_index_img/8AcQkc.png`)
          location.href = `https://pmp-preview.itheima.net/j8rXem/?${query}`
        } else {
          // 开发环境直接跳转到首页
          this.$router.push({
            path: this.redirect || '/',
            query: this.otherQuery
          })
        }
        setTimeout(() => {
          this.loading = false
        }, 0.5 * 1000)
      } else {
        return false
      }
    })
  }

  private getOtherQuery(query: Dictionary<string>) {
    return Object.keys(query).reduce((acc, cur) => {
      if (cur !== 'redirect') {
        acc[cur] = query[cur]
      }
      return acc
    }, {} as Dictionary<string>)
  }
}
</script>
<style lang="scss">
.divLogin {
  .divLeft {
    .el-input__inner {
      width: 230px;
      height: 50px;
      background-color: #f8f5f5;
      border-bottom-left-radius: 0;
      border-top-left-radius: 0;
    }
  }
}
</style>
<style lang="scss" scoped>
.divLogin {
  background: #fdfafa;
  .divContent {
    width: 958px;
    height: 550px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0px 20px #5d5d5d54;
    background-color: #ffffff;
    border-radius: 40px;
    .divLeft {
      float: left;
      width: 398px;
      height: 550px;
      background: #ffffff;
      border-radius: 34px 0px 0px 34px;
      margin: 0 auto;
      .divTitle {
        margin-left: 124px;
        margin-top: 72px;
        img {
          width: 150px;
          height: 64px;
        }
      }
      ::v-deep  .el-form-item__error{
          margin-left: 110px;
      }
      .divUser,
      .divPwd {
        margin-left: 59px;
        width: 280px;
        height: 50px;
        .divImg {
          float: left;
          width: 50px;
          height: 50px;
          background: #eae7e7;
          border-radius: 8px 0px 0px 8px;
        }
        .divInput {
          float: left;
        }
      }
      .divUser {
        margin-top: 50px;
        img {
          width: 25px;
          height: 24px;
          margin: 12px 13px;
        }
      }
      .divPwd {
        margin-top: 22px;
        img {
          width: 24px;
          height: 24px;
          margin: 13px;
        }
      }
      .divCheckBox {
        margin-top: 22px;
        margin-left: 60px;
      }
      .divBtnLogin {
        margin-top: 23px;
        margin-left: 59px;
        .el-button {
          width: 280px;
          height: 50px;
          background: #ffb200;
          border-radius: 8px;
          box-shadow: 0px 2px 9px 1px rgba(255, 178, 0, 0.47);
          font-size: 16px;
          font-weight: 600;
          text-align: center;
          color: #332929;
          line-height: 22px;
        }
      }
    }
    .divRight {
      float: right;
      background-image: url(~@/assets/login/contentBg.png);
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
      width: 560px;
      height: 516px;
    }
  }
}
.gentleReminder{
    line-height: 20px;
    font-size: 14px;
    color: #FD3333;
    padding: 20px 60px 0;
    text-align: justify;
  }
</style>
